package com.google.coffeeshop.dao.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.google.coffeeshop.dao.OrderDAO;
import com.google.coffeeshop.entity.OrderDTO;
import com.google.coffeeshop.entity.UserDTO;
import com.googlecode.genericdao.search.Search;


public class OrderDAOImpl extends AbstractBaseDAOImpl<OrderDTO, Long> implements OrderDAO {
	public List<OrderDTO> findBetweenDates(Date beginDate, Date endDate) {
		Search search = new Search(OrderDTO.class);
		search.setFirstResult(0);
		search.setMaxResults(Integer.MAX_VALUE);
		search.addFilterGreaterOrEqual("createDate", beginDate);
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(endDate);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		search.addFilterLessOrEqual("createDate", calendar.getTime());
		List<OrderDTO> orders= this.search(search);
		if(null == orders || orders.isEmpty())
			return null;
		return orders;
	}
	
	public List<OrderDTO> findByDate(Date date) {
		Search search = new Search(OrderDTO.class);
		search.setFirstResult(0);
		search.setMaxResults(Integer.MAX_VALUE);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		search.addFilterLessOrEqual("createDate", calendar.getTime());
		List<OrderDTO> orders = this.search(search);
		if(null == orders || orders.isEmpty())
			return null;
		return orders;
	}

	@Override
	public OrderDTO findBySeatId(long seatId, Integer statusId) {
		Search search = new Search(OrderDTO.class);
		search.setFirstResult(0);
		search.setMaxResults(1);
		search.addFilterEqual("seat.id", seatId);
		search.addFilterEqual("status", statusId);
		return this.searchUnique(search);
	}

	@Override
	public List<OrderDTO> findAllBySeatId(long seatId, Integer statusId) {
		Search search = new Search(OrderDTO.class);
		search.setFirstResult(0);
		search.setMaxResults(Integer.MAX_VALUE);
		search.addFilterEqual("seat.id", seatId);
		search.addFilterEqual("status", statusId);
		return this.search(search);
	}
}
